<?php
/**
 * 个人考勤详情
 */
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class('Tools_Tools');
apf_require_class('Bll_Attendance_UserInfo');
class Oa_Attendance_UserDetailController extends Oa_Attendance_AbstractController {
    const PAGEROW = 8;
    public function handle_request() {
        $params = $this->request->get_parameters();

        $user_id = $params['uid'];
        $yearmonth = $params['ym'];
        //查看权限
        if(!$this->check($user_id,$yearmonth)){
            $params['uid'] = $_SESSION['user_id'];
        }
        //echo $user_id;exit;
        //format start end
        $params['start'] = $params['ym'].'01';
        $p_time= strtotime($params['start']);
        $p_days = date('t',$p_time);
        $params['end'] =$params['ym'].$p_days;

        if(empty($user_id)) {
            $user_id = $this->uinfo['user_id'];
            $params['uid'] = $user_id;
        }
        //获得用户基本信息
        $bll_userinfo = $this->init_userinfo();
        $user_info = $bll_userinfo->get_user_by_ids($params['uid']);
        $user_info = $user_info['0'];
        $this->request->set_attribute('the_user_info',$user_info);

        //初始化查询时间
        $time = $this->get_time($yearmonth);
        $start = $time['start'];
        $end = $time['end'];

        //显示时间
        $this->request->set_attribute('nowtime',date('Y年m月',strtotime($start)));

        //获取是否有下级信息,居然是为了显示加班工时.....
        $userdata=array();
        if($_SESSION['user_id'] != $params['uid']){
            $userdata=array(1);
            //apf_require_class('Bll_Attendance_OrgRelation');
            //$user_org = new Bll_Attendance_OrgRelation();
            //$userdata = $user_org->get_subordinate_by_user($_SESSION['user_id']);
            //if(empty($userdata)){
            #apf_require_class('Bll_Login_User');
            #$user_bll = new Bll_Login_User;
            #$role_id = $user_bll->get_user_role_id($_SESSION['code']);
            #if($role_id==3){
            #    $userdata = array(3);
            #}
            #if($_SESSION['role_id']==3){
            #    $userdata = array(3);
            #}test
            $userdata = array(3);
            $this->request->set_attribute('is_self_see',true);
        }

        //获得员工考勤明细
        $bll_dayrecord = $this->init_AccessDayRecord();
        if($this->oa_datatype){
            $result = $this->get_attendance_list_new_new($params);
        }else{
            //$result = $bll_dayrecord->get_my_attendance($user_id,$start,$end);
            $result = $this->get_attendance_list_new($params,$user_id,$user_info);
        }
        //1.迟到，2.早退,3.缺勤,这个是ok的。
 /*       foreach ($result as $key => $value){
            if (!empty($result[$key]['status']) && $this->uinfo['user_type'] == 1) {
                if ($result[$key]['status'][0]['id'] == 1 || $result[$key]['status'][1]['id'] == 2 || $result[$key]['status'][0]['name'] == '上午缺勤' || $result[$key]['status'][1]['name'] == '下午缺勤') {
                    $result[$key]['status'][0]['id'] = 999;
                    $result[$key]['status'][0]['name'] = '正常';
                    unset($result[$key]['status'][1]);
                }
            }
        }
*/
        unset($result['allleaverecord']);
        $this->request->set_attribute('userdata',$userdata);

        $this->request->set_attribute('result',$result);
        $this->request->set_attribute('params',$params);
        if($this->oa_datatype){
            return 'Oa_Attendance_UserDetailNew';
        }else{
            return 'Oa_Attendance_UserDetail';
        }
    }

    /**
     * 时间格式处理,获得开始时间和结束时间
     */
    public function get_time($time = 0) {
        $result = array('start'=>0,'end'=>'0');
        if(empty($time)) {
            $time = date('Ym');
        }
        $time = $time.'01';

        $time = strtotime($time);
        $month = date('m',$time);
        $start = date('Ymd',mktime(0,0,0,$month,1,date('Y',$time)));//本月开始
        $end = date('Ymd',mktime(0,0,0,$month+1,1,date('Y',$time))-86400);//结束日期
        $result['start'] = $start;
        $result['end'] = $end;
        return $result;
    }

    /**
     * userdetail/?uid=1&ym=201206
     */
    public function build_url($uid,$time = 0) {
        $url = APF::get_instance()->get_config('base_domain');
        if(!empty($uid)){
            $url = $url.'/userdetail/?uid='.$uid.'&ym='.$time;
        }
        return $url;
    }

    /**
     * 根据时间点本月当天前的数据
     * access_date >= $start AND access_date <= $end
     * @param mixed $params
     * @access private
     * @return void
     */
    private function get_attendance_list($params) {
        $result = array();
        //如果用户未登录，返回空数组
        $uid = $params['uid'];
        $ym = $params['ym'];

        $y = substr($ym,0,4);
        $m = substr($ym,4,2);

        $dayinfo = getdate(mktime(0,0,0,$m+1,1,$y)-1);
        $m_lenght = $dayinfo['mday'];        //当月最后一天

        $now_ym = date("Ym");
        if($now_ym == $ym){
            $m_lenght = date("d");//当月做特殊处理
        }
        elseif($now_ym < $ym  or !$uid){//大于当月的没有数据,无uid
            return array();
        }

        apf_require_class('Bll_Login_User');
        $user_bll = new Bll_Login_User;
        $uinfo = $user_bll->get_user_info_by_ids(array($uid));
        $uinfo = $uinfo[$uid];
        apf_require_class('Tools_Tools');

        $base = $base * 7;
        $keep = 0;

        $start = $ym."01";
        $end = $ym.$m_lenght;
        $te = mktime(0,0,0,$m,$m_lenght,$y);

        $bll = new Bll_Attendance_AccessDayRecord();
        $list = $bll->get_my_attendance($uid,$start,$end);

        $leave_record = $list['allleaverecord'];unset($list['allleaverecord']);
        $first = $te;
        for($i = 0; $i < $m_lenght; $i++) {
            $each = mktime(0,0,0,date('m',$te),date('d',$te) - $i,date('Y',$te));
            $curr = date('Ymd',$each);
            //如果输入了起始时间
            if($keep && $curr < $keep) {
                break;
            }
            //只开启上海
            if($uinfo['city_name'] != '上海') {
                $tmp = array();
                $tmp = array('month' => date('m',$each),
                        'day' => date('d',$each),
                        'week' => Tools_Tools::get_instance()->convert_chinese_week(date('w',$each))
                );
                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                $result[]  = $tmp;
                continue;
            }
            if($list[$curr]) {
                $result[] = $list[$curr];
            } else {
                $tmp = array();
                $tmp = array('month' => date('m',$each),
                        'day' => date('d',$each),
                        'week' => Tools_Tools::get_instance()->convert_chinese_week(date('w',$each))
                );
                //TODO 处理工作日，没有记录的情况 ,周末已加，请加销售人员特殊处理
                //不为周末时的情况
                if(date('w',$each) != 6 && date('w',$each) != 0) {
                    $holiday = Tools_Tools::get_instance()->each_year_holiday_country();
                    if(in_array(date('Y-m-d',$each),$holiday)) {
                        $tmp['status'][0] = array('id' => 999,'name' => '法定节假日');
                    }
                    elseif(empty($leave_record)) {
                        $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                        if(date('Ymd',$each) >= 20120620) {
                            $tmp['status'][0] = array('id' => 3,'name' => '缺勤');;
                        }
                    } else {
                        $find =false;
                        foreach ($leave_record as $k => $v) {
                            $ajust = mktime(9,31,0,date('m',$each),date('d',$each),date('Y',$each));
                            if($v['type'] == '缺勤' or $v['type'] == '迟到' or $v['type'] == '早退') {
                                $v['end'] += 86399;
                            }
                            if($ajust >= $v['start'] && $ajust <= $v['end']) {
                                $tmp['status'][0] = array('id' => 999,'name' => $v['leave_reason'],'status'=>"({$v['status']})");
                                $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                $reason = "类别:".$v['type'].$desc;
                                //病假，陪产假特殊处理
                                if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                                    if((time() - $v['apply_time_ext']) < 604800) {
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['type'].$desc."  <a href='/proof/?id=".$v['id']."'>上传证明</a>";
                                    }
                                }
                                if(($v['type'] == '病假' || $v['type'] == '陪产假') && $v['img']) {
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['type'].$desc."  <a href='/upload/attendance/".$v['img']."' target='_blank'>查看证明</a>";
                                }
                                $tmp['reason'][] = $reason;
                                $find = true;
                                break;
                            }
                        }
                        if(!$find) {
                            $tmp['status'][0] = array('id' => 3,'name' => '缺勤');
                            //从20120601开始，上线则需改为0704
                            if(date('Ymd',$each) < 20120704) {
                                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                            } elseif(date('H') >= 9 && date('s') >= 31 && date('s') <= 40) {
                                //9点11后显示迟到
                                $tmp['status'][0] = array('id' => 1,'name' => '迟到');
                            } elseif(date('H') >= 9 && date('s') > 40) {
                                //9点30后显示缺勤
                            } elseif (date('Ymd',$each) === date('Ymd')) {
                                $tmp['status'][0] = array('id' => 999,'name' => '考勤数据准备中');
                            }
                        }
                    }
                }
                $result[] = $tmp;
            }
        }
/*
        foreach ($result as $key => $value){
            if (!empty($result[$key]['status']) && $this->uinfo['user_type'] == 1) {
                if ($result[$key]['status'][0]['id'] == 1 || $result[$key]['status'][1]['id'] == 2 || $result[$key]['status'][0]['name'] == '上午缺勤' || $result[$key]['status'][1]['name'] == '下午缺勤') {
                    $result[$key]['status'][0]['id'] = 999;
                    $result[$key]['status'][0]['name'] = '正常';
                    unset($result[$key]['status'][1]);
                }
            }
        }*/
        return $result;
    }
    private function get_attendance_list_new($params,$user_id,$uinfo) {
        $result = array();

        apf_require_class('Tools_Tools');
        $start = 0;
        //分页基数
        $base = 0;
        $preurl = "?1=1";
        //    echo ($params['start']);die();
        $preurl .= isset($params['start'])?"&start=".$params['start']:"";
        $preurl .= isset($params['end'])?"&end=".$params['end']:"";
        $this->page['pre'] = $preurl."&p=0";
        $this->page['next'] = $preurl."&p=2";
        if(date('H') > 0 && date('H') < 12) {
            $ma = '上午';
        } else {
            $ma = '下午';
        }
        $this->page['curr_time'] = date('Y').'年'.date('m').'月'.date('d')."日&nbsp;&nbsp;&nbsp;星期".Tools_Tools::get_instance()->convert_chinese_week(date('w'))."&nbsp;&nbsp;&nbsp;&nbsp;".$ma."&nbsp;&nbsp;&nbsp;".date('h:i');
        if($params['p'] && $params['p'] > 1) {
            $base = $params['p'] - 1;
            $this->page['pre'] = $preurl."&p=".$base;
            $nz = $base + 2;
            $this->page['next'] = $preurl."&p=".$nz;
        }
        $base = $base * 7;
        $keep = 0;
        if(empty($params['start']) && empty($params['end'])) {
            //分页逻辑
            $te = mktime(0,0,0,date('m'),date('d') - $base,date('Y'));
            $end = date('Ymd',$te);//从今天开始倒序排列
            $seven = mktime(0,0,0,date('m',$te),1,date('Y',$te));
            $start = date('Ymd',$seven);
        }
        if($params['end'] || $params['start']) {
            $ts = $params['start']?strtotime($params['start']):0;
            $keep = date('Ymd',$ts);
            $te = $params['end']?strtotime($params['end']):0;
            $te = mktime(0,0,0,date('m',$te),date('d',$te) - $base,date('Y',$te));
            if(date('Ymd',$te) > date('Ymd')) {
                $te = time();
            }
            $end = date('Ymd',$te);
            $seven = mktime(0,0,0,date('m',$te),1,date('Y',$te));
            $start = date('Ymd',$seven);
            if($keep > $start) {
                $start = $keep;
            }
        }

        $end_days = intval(substr($end,6));
        $bll = new Bll_Attendance_AccessDayRecord();
        $list = $bll->get_my_attendance($user_id,$start,$end);
      //  print_r($list);
        $leave_record = $list['allleaverecord'];unset($list['allleaverecord']);
        //处理周末上班
        $work_weekend = Tools_Tools::get_instance()->special_work_day();
        //处理特殊下班时间
        $special_leave_times = Tools_Tools::get_instance()->special_leave_time();
        foreach($special_leave_times as $s_index => $s_value){
            if(!in_array($s_index, $work_weekend)){
                $work_weekend[]=$s_index;
            }
        }

        $first = $te;
        for($i = 0; $i < $end_days; $i++) {
            $each = mktime(0,0,0,date('m',$te),date('d',$te) - $i,date('Y',$te));

            $curr = date('Ymd',$each);
            //如果输入了起始时间
            if($keep && $curr < $keep) {
                break;
            }

            if($list[$curr]) {
                $result[] = $list[$curr];
            } else {
                $tmp = array();
                $tmp = array('month' => date('m',$each),
                        'day' => date('d',$each),
                        'week' => Tools_Tools::get_instance()->convert_chinese_week(date('w',$each))
                );
                //TODO 处理工作日，没有记录的情况 ,周末已加，请加销售人员特殊处理
                //不为周末时的情况
                if(in_array(date('Y-m-d',$each), $work_weekend) || (date('w',$each) != 6 && date('w',$each) != 0)) {
                    $holiday = Tools_Tools::get_instance()->each_year_holiday_country();
                    $take_holiday = Tools_Tools::get_instance()->holiday_country();//倒休的日期
                    if(in_array(date('Y-m-d',$each), $work_weekend)){
                        $tmp['sp_work_day'] = 1;
                    }
                    if(in_array(date('Y-m-d',$each),$holiday) || in_array(date('Y-m-d',$each),$take_holiday)) {
                        $tmp['status'][0] = array('id' => 999,'name' => '法定节假日');
                    }
                    elseif(empty($leave_record)) {
                        $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                        if(date('Ymd',$each) >= 20120620) {
                            $tmp['status'][0] = array('id' => 3,'name' => '全天缺勤');;
                        }
                        if(date('Ymd',$each) <= date('Ymd',strtotime($uinfo['enter_date']))) {
                            $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                        }
                        if (date('Ymd',$each) == date('Ymd') && date('Hi') >= 931) {
                            $tmp['status'][0] = array('id' => 3,'name' => '上午缺勤');
                        }
                    } else {
                        $find =false;
                        $que_keep = array();
                        foreach ($leave_record as $k => $v) {
                            if($v['status'] != '已批准' && $v['status'] != '未审核') {
                                continue;
                            }
                            $ajust = mktime(9,31,0,date('m',$each),date('d',$each),date('Y',$each));
                            $afternoon = mktime(14,0,0,date('m',$each),date('d',$each),date('Y',$each));
                            $oafter = mktime(14,0,0,date('m',$each),date('d',$each),date('Y',$each));
                            $end_time = mktime(18,0,0,date('m',$each),date('d',$each),date('Y',$each));
                            //缺勤数据库里已记录时间 add 20121016
                            if($v['leave_reason'] == '地铁故障') {
                                $v['end'] += 43200;
                            } elseif(($v['type'] == '迟到' || $v['type'] == '缺勤') && $v['leave_reason'] == '忘记带卡') {
                                $v['start']= mktime(9,0,0,date('m',$v['start']),date('d',$v['start']),date('Y',$v['start']));
                                $v['end']= mktime(18,0,0,date('m',$v['start']),date('d',$v['start']),date('Y',$v['start']));
                            } elseif($v['type'] == '迟到' && $v['leave_reason'] != '忘记带卡') {
                                $v['end'] += 43200;
                            } elseif ($v['type'] == '早退') {
                                $v['start'] += 43200;
                                $v['end'] += 86399;
                            } elseif ($v['type'] == '缺勤' && ($v['leave_reason'] == '其它' || $v['leave_reason'] == '忘记刷卡')) {
                                /*$pre_day = date('Y-m-d',$v['start']);
                                if(in_array($pre_day, $que_keep)) {
                                    $v['start'] += 50800;
                                    $v['end'] += 86399;
                                } else {
                                    $v['end'] += 43200;
                                }
                                $que_keep[] = $pre_day;*/
                            } elseif($v['type'] == '缺勤') {
                                //$v['end'] += 86399;
                            }

                            if($ajust >= $v['start'] && $oafter < $v['end'] && $v['type']!='公出') {//所有的公出都进行上午的方式进行判断 20121025

                                $tmp['status'][0] = array('id' => 999,'name' => $v['leave_reason'],'status'=>"({$v['status']})");
                                if($v['end']<$end_time && date('Ymd')!=$curr){
                                    $tmp['status'][1] = array('id' => 3,'name' => '下午缺勤');
                                }
                                $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                $reason = "类别:".$v['type'].$desc;
                                //病假，陪产假特殊处理
                                if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                                    if((time() - $v['apply_time_ext']) < 604800) {
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['type'].$desc."  <a href='/proof/?id=".$v['id']."'>上传证明</a>";
                                    }
                                }
                                if(($v['type'] == '病假' || $v['type'] == '陪产假') && $v['img']) {
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['type'].$desc."  <a href='/upload/attendance/".$v['img']."' target='_blank'>查看证明</a>";
                                }
                                $tmp['reason'][] = $reason;
                                $find = true;
                                if($v['end']>=$end_time){
                                    unset($tmp['status'][1]);
                                }
                                //判断特殊班别
                                if($this->uinfo['user_type'] == 1 && date('Ymd',$each) >= $this->uinfo['user_type_date'] && ($tmp['status'][0]['id']==999 || $tmp['status'][1]['id']==999)) {
                                    $tmp['status'][0] = array('id' => 999,'name' => '正常');
                                    $tmp['status'][1] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                                    $find=true;
                                }
                                //结束判断特殊班别
                                break;
                            } else {
                                //echo date('Y-md H:i',$ajust)."==".date('Y-md H:i',$oafter)."=".date('Y-md H:i',$v['start'])."=".date('Y-md H:i',$v['end'])."==============<br>";
                                //拆分上下午
                                if(!isset($tmp['status'][0])) {
                                    $tmp['status'][0] = array('id' => 3,'name' => '上午缺勤');
                                }
                                if(!isset($tmp['status'][1])) {
                                    $tmp['status'][1] = array('id' => 3,'name' => '下午缺勤');
                                }
                                $curr_date = (date('Ymd',$each) == date('Ymd',$v['start']) || date('Ymd',$each) == date('Ymd',$v['end']));
                                if($ajust >= $v['start'] && $v['end'] <= $afternoon && $curr_date) {
                                    $tmp['status'][0] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['leave_reason'].$desc;
                                    //病假，陪产假特殊处理
                                    if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                                        if((time() - $v['apply_time_ext']) < 604800) {
                                            $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                            $reason = "类别:".$v['type'].$desc."  <a href='/proof/?id=".$v['id']."'>上传证明</a>";
                                        }
                                    }
                                    if(($v['type'] == '病假' || $v['type'] == '陪产假') && $v['img']) {
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['type'].$desc."  <a href='/upload/attendance/".$v['img']."' target='_blank'>查看证明</a>";
                                    }
                                    $tmp['reason'][] = $reason;
                                    $find = true;
                                }
                                if($v['start'] >= $afternoon && $v['end'] >= $end_time && $v['leave_reason'] != '地铁故障' && $curr_date) {
                                    $tmp['status'][1] = array('id' => 999,'name' => $v['leave_reason'],'status'=>"({$v['status']})");
                                    $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                    $reason = "类别:".$v['type'].$desc;
                                    //病假，陪产假特殊处理
                                    if(($v['type'] == '病假' || $v['type'] == '陪产假') && empty($v['img'])){
                                        if((time() - $v['apply_time_ext']) < 604800) {
                                            $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                            $reason = "类别:".$v['type'].$desc."  <a href='/proof/?id=".$v['id']."'>上传证明</a>";
                                        }
                                    }
                                    if(($v['type'] == '病假' || $v['type'] == '陪产假') && $v['img']) {
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['type'].$desc."  <a href='/upload/attendance/".$v['img']."' target='_blank'>查看证明</a>";
                                    }
                                    $tmp['reason'][] = $reason;
                                    $find = true;
                                }
                                if($v['type']=='公出'){
                                    if($v['end'] >= $end_time && $curr_date){
                                        $tmp['status'][1] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['leave_reason'].$desc;
                                        $tmp['reason'][] = $reason;
                                        $find = true;
                                    }
                                    $ajust_9 = mktime(9,0,0,date('m',$each),date('d',$each),date('Y',$each));
                                    if($v['start'] >= $ajust_9 && $curr_date){
                                        $tmp['status'][0] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                                        $desc = empty($v['leave_desc'])?'':"  备注:".$v['leave_desc'];
                                        $reason = "类别:".$v['leave_reason'].$desc;
                                        $tmp['reason'][] = $reason;
                                        $find = true;
                                    }
                                }
                                if(count($tmp['status']) == 2 && count($tmp['reason']) < 1) {
                                    $find =  false;
                                    unset($tmp['status']);
                                }
                                //判断特殊班别
                                if($this->uinfo['user_type'] == 1 && date('Ymd',$each) >= $this->uinfo['user_type_date'] && ($tmp['status'][0]['id']==999 || $tmp['status'][1]['id']==999)) {
                                    if(date('Ymd',$each)>=date('Ymd',$v['start']) && date('Ymd',$each)<=date('Ymd',$v['end'])){
                                        $tmp['status'][0] = array('id' => 999,'name' => '正常');
                                        $tmp['status'][1] = array('id' => 999,'name' => $v['type'],'status'=>"({$v['status']})");
                                    }
                                    $find=true;
                                }
                                //add 20121025
                                $tmp['reason'] = array_unique($tmp['reason']);
                                //结束判断特殊班别
                            }

                        }
                        if(!$find) {
                            $tmp['status'][0] = array('id' => 3,'name' => '全天缺勤');
                            if(date('Ymd',$each) <= date('Ymd',strtotime($uinfo['enter_date']))) {
                                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                            }

                            //只开启上海
                            if($this->uinfo['city_name'] != '上海') {
                                $tmp = array();
                                $tmp = array('month' => date('m',$each),
                                        'day' => date('d',$each),
                                        'week' => Tools_Tools::get_instance()->convert_chinese_week(date('w',$each))
                                );
                                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                                $result[]  = $tmp;
                                continue;
                            }

                            //从20120601开始，上线则需改为0620
                            if(date('Ymd',$each) < 20120620) {
                                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                            } elseif(date('Ymd',$each) === date('Ymd') && date('Hi') >= 931 && date('Hi') <= 940) {
                                //9点11后显示迟到
                                $tmp['status'][0] = array('id' => 1,'name' => '迟到');
                            } elseif(date('Ymd',$each) === date('Ymd') && date('Hi') >= 941 && date('Hi') <= 2359) {
                                $tmp['status'][0] = array('id' => 3,'name' => '上午缺勤');
                            } elseif (date('Ymd',$each) === date('Ymd') && date('Hi') > 1431 && date('Ymd',$each) != date('Ymd')) {
                                $tmp['status'][0] = array('id' => 3,'name' => '全天缺勤');
                            } elseif (date('Ymd',$each) === date('Ymd') && date('Hi') <= 941) {
                                $tmp['status'][0] = array('id' => 999,'name' => '考勤数据准备中');
                            } elseif ($each <= (strtotime($this->uinfo['enter_date'])+86400)) {
                                $tmp['status'][0] = array('id' => 999,'name' => '无考勤数据');
                            }
                            if($this->uinfo['user_type'] == 1 && date('Ymd',$each) >= $this->uinfo['user_type_date'] && date('Ymd',$each)==date('Ymd')){
                                $tmp['status'][0] = array('id' => 999,'name' => '考勤数据准备中');
                            }
                        }
                    }
                }
                $tmp['status_kq'][0] = $tmp['status'][0];
                $result[] = $tmp;
            }
        }
        if(empty($result) || count($result) < 7) {
            $this->page['pre'] = 0;
            $this->page['next'] = 0;
        }
        //特殊班别判断
        /*foreach ($result as $key => $value){
            if (!empty($result[$key]['status']) && $uinfo['user_type'] == 1) {
                if ($result[$key]['status'][0]['id'] == 1 || $result[$key]['status'][1]['id'] == 2 || $result[$key]['status'][0]['name'] == '上午缺勤' || $result[$key]['status'][1]['name'] == '下午缺勤') {
                    $result[$key]['status'][0]['id'] = 999;
                    $result[$key]['status'][0]['name'] = '正常';
                    unset($result[$key]['status'][1]);
                }
            }
        }*/

        return $result;
    }
    /**
     * 根据时间点本月当天前的数据
     * access_date >= $start AND access_date <= $end
     * @param mixed $params
     * @access private
     * @return void
     */
    private function get_attendance_list_new_new($params) {
        apf_require_class('Bll_Attendance_DailyLeave');
        $result = array();
        //如果用户未登录，返回空数组
        $uid = $params['uid'];
        $ym = $params['ym'];

        $y = substr($ym,0,4);
        $m = substr($ym,4,2);

        $dayinfo = getdate(mktime(0,0,0,$m+1,1,$y)-1);
        $m_lenght = $dayinfo['mday'];        //当月最后一天

        $now_ym = date("Ym");
        if($now_ym == $ym){
            $m_lenght = date("d");//当月做特殊处理
        }
        elseif($now_ym < $ym  or !$uid){//大于当月的没有数据,无uid
            return array();
        }

       /*  apf_require_class('Bll_Login_User');
        $user_bll = new Bll_Login_User;
        $uinfo = $user_bll->get_user_info_by_ids(array($uid));
        $uinfo = $uinfo[$uid]; */
        //用户存在，有查看权限

        $start = $ym."01";
        $end = $ym.$m_lenght;

        $bll = new Bll_Attendance_DailyLeave();
        $result = $bll->get_attendance_list($uid,$start,$end);
        return $result;
    }

    private function check($uid,$ym){
        apf_require_class("Bll_Login_User");
        //-----------------对hr做特殊处理
        $user_bll = new Bll_Login_User;
        $role_id = $user_bll->get_user_role_id($_SESSION['code']);
        if($role_id == 3){
            return true;
        }
        //-----------------对hr做特殊处理
        apf_require_class("Bll_Attendance_Report");
        apf_require_class("Bll_Attendance_UserInfo");
        $bll_report = new Bll_Attendance_Report();
        $bll_report->set_bll_user(new Bll_Attendance_UserInfo());
        $params = array(
                'select_year'=>intval(substr($ym,0,4)),
                'select_month'=>substr($ym,4),
                'offset'=>0,
                'row'=>0,
                'level'=>'all',
        );
        $users = $bll_report->get_all_user_info($params,$_SESSION['user_id']);
        $user_ids = array();
        $user_ids[] = $_SESSION['user_id'];
        foreach($users['data'] as $user){
            $user_ids[] = $user['user_id'];
        }
        if(in_array($uid, $user_ids)){
            return true;
        }
        return false;
    }
}
